<?php
namespace models\repositories;
use Doctrine\ORM\EntityRepository;
use entities;
class BarangItemRepository extends EntityRepository {
	
	public function paginateBarang($indexAndCount=NULL, $orders=NULL){
		$total = $this->countAll();	
		if ($indexAndCount != NULL){			
			$index = $indexAndCount[0];
			$limit = $indexAndCount[1];
			$offset = ($index - 1 ) * $limit;
			
			if ($offset>0)
				$indexAndCount[0] = $offset;
			
		}		
		
		$qb = $this->_em->createQueryBuilder();
				
		$qb->select('Barang')
		   ->from('models\entities\Barang', 'Barang');
		  
		if ($orders != NULL && is_array($orders)){
			$qb->orderBy($orders[0]['fieldName'], $orders[0]['orderType']);			
			if (count($orders) > 1){
				for ($i=1;$i<=count($orders);$i++){
					$qb->addOrderBy($orders[$i]['fieldName'], $orders[$i]['typeOrder']);
				} 
			} 
		}   
		   
		if ($indexAndCount != NULL){
			$qb->setFirstResult( $offset )
   		   	   ->setMaxResults( $limit );
		}   
			
		$query = $qb->getQuery(); 
		$result['data'] = $query->getResult();
		$result['count']= $total;
		$result['indexAndCount'] = $indexAndCount;
		return $result;
	}
	
	public function paginateByMap($map, $indexAndCount=NULL, $orders=NULL){
			
		if ($indexAndCount != NULL){			
			$index = $indexAndCount[0];
			$limit = $indexAndCount[1];
			$offset = ($index - 1 ) * $limit;
			
			if ($offset>0)
				$indexAndCount[0] = $offset;
			
		}		
		
		$qb = $this->_em->createQueryBuilder();
		
		$qb->select('Barang')
		   ->from('models\entities\Barang', 'Barang');
		$searchStatement = "";
		   	 
		$whereClause = "";   	 
		if (is_array($map['pFieldName'])){
			$i=0; 
			foreach ($map['pFieldName'] as $field){
				$dot = explode(".", $field);
				$sub = substr($field, -2);
				if (strtoupper($sub) == "ID" && count($dot)>2){
					die("l");
					$tfield = substr($field,0, strlen($field) - 3);
					$tfield ="IDENTITY({$tfield})";
				} else {
					$tfield = $field;
				}

				$where = "";
				switch ($map['pCondition'][$i]){
					case "LIKE" : $where = "{$field} ".$map['pCondition'][$i]."?".$i;
					break;
					case "EQUAL" : $where = "{$field} = ?".$i;
					break; 
					case "NOT_EQUAL" : $where = "{$field} != ?".$i;
					break;
				}
				if ($i==0){
					$whereClause = $whereClause . $where;
				} else {
					$whereClause = $whereClause . " and " .$where;	
				}
				if ($map['pCondition'][$i] == "LIKE"){
					$qb->setParameter($i, "%".$map['pFieldValue'][$i]."%");
				} else {
					$qb->setParameter($i, $map['pFieldValue'][$i]);	
				} 				
				$i++;
			}
	
			$qb->where($whereClause);
			// die($whereClause);
		}
		
		// echo "<pre>";print_r($qb->getQuery()->getSQL());die();
		
		$total = $this->countAllByMap($whereClause, $map);
		  
		if ($orders != NULL && is_array($orders)){
			$qb->orderBy($orders[0]['fieldName'], $orders[0]['orderType']);			
			if (count($orders) > 1){
				for ($i=1;$i<=count($orders);$i++){
					$qb->addOrderBy($orders[$i]['fieldName'], $orders[$i]['typeOrder']);
				} 
			} 
		}   
		   
		if ($indexAndCount != NULL){
			$qb->setFirstResult( $offset )
   		   	   ->setMaxResults( $limit );
		}   
			
		$query = $qb->getQuery(); 
		$result['data'] = $query->getResult();
		$result['count']= $total;
		$result['indexAndCount'] = $indexAndCount;
		return $result;
	}

	public function countAll(){
		$qb = $this->_em->createQueryBuilder();		
		$qb->select('count(a.id)')
		   ->from('models\entities\Barang', 'a')	   		
		;		
		
		$count = $qb->getQuery()->getSingleScalarResult();
		return $count;
	}
	
	public function countAllByMap($whereClause, $map){
		$qb = $this->_em->createQueryBuilder();		
		$qb->select('count(Barang.id)')
		   ->from('models\entities\Barang', 'Barang')	   		
		;
		if ($whereClause != ""){
			$qb->where($whereClause);
		}
		$i=0;
		foreach ($map['pFieldValue'] as $p){
			if ($map['pCondition'][$i] == "LIKE"){
				$qb->setParameter($i, "%".$p."%");
			} else {
				$qb->setParameter($i, $p);	
			}
			$i++;	
		}
		
		$count = $qb->getQuery()->getSingleScalarResult();
		return $count;
	}
	
	
}

